//
//  CZWelcomeViewController.swift
//  1215微博终极项目
//
//  Created by chok崋 on 15/12/17.
//  Copyright © 2015年 周卓华. All rights reserved.
//

import UIKit

class CZWelcomeViewController: UIViewController {

    //定义头像的约束
    private var iconButtomCon:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
     }
    
    //当view看到的时候做动画
    override func viewDidAppear(animated: Bool) {
        iconButtomCon?.constant = -(UIScreen .mainScreen().bounds.height - 160)
        //添加动画
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue:0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //隐藏UILabel
                self.welcomeLbl.alpha = 0
                //添加UILable动画
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLbl.alpha = 1
                    self.welcomeLbl.hidden = false
                    }, completion: { (_) -> Void in
                        print("所有动画完成")
                        
                        //设置动画完成跳转到主界面
                        (UIApplication.sharedApplication().delegate as!AppDelegate).switchRootViewController(true)
                })
        }
    }

    //准备UI
    func prepareUI()
    {
      view.addSubview(backGround)
      view.addSubview(iconHead)
      view.addSubview(welcomeLbl)
        
     //清除自动布局
        backGround.translatesAutoresizingMaskIntoConstraints = false
        iconHead.translatesAutoresizingMaskIntoConstraints = false
        welcomeLbl.translatesAutoresizingMaskIntoConstraints = false
        
    //添加背景约束
        view.addConstraint(NSLayoutConstraint(item: backGround, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backGround, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backGround, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backGround, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //添加头像的约束
        view.addConstraint(NSLayoutConstraint(item: iconHead, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //记录底部的约束
        iconButtomCon = NSLayoutConstraint(item: iconHead, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconButtomCon!)
        //添加lable的约束
        view.addConstraint( NSLayoutConstraint(item: welcomeLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconHead, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        view.addConstraint( NSLayoutConstraint(item: welcomeLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconHead, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
       
        
    }
    
    
  
    //懒加载
    lazy var backGround:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    lazy var iconHead:UIImageView = {
            let imageView = UIImageView(image: UIImage(named:"avatar_default_big" ))
        
        //将圆形切片
        imageView.layer.cornerRadius = 42.5 //图片除以2得到半径，在除以2，得到切片
        imageView.layer.masksToBounds = true
        
        return imageView;
       
    }()
    //欢迎lable
    lazy var welcomeLbl:UILabel = {
        let lbl = UILabel()
        lbl.text = "欢迎归来"
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.hidden = true
        return lbl
    
    }()
    
}













